import json

def convert_csv_to_json(file_name,save_file_name):
    result = []
    key_list = None
    temp_str = ""
    count = 0
    with open(file_name,'r',encoding='utf-8') as fread:
        for index,line in enumerate(fread):
            line = line.strip()
            if not line:
                continue
            if index == 0:
                key_list = line.strip().split(",")
            else:
                temp_str +=line +"\n"
                if "\"" in line:
                    if count == 0:
                        count += 1
                    else:
                        temp_str = temp_str[:-1]
                        count -= 1


                        value_list = temp_str.strip().split(",")
                        temp_str = ""
                        item = {}
                        for (key,value) in zip(key_list,value_list):
                            value = value.replace("\"","")
                            if key != "label":
                                item[key] = value
                            else:
                                item[key] = [value]
                        result.append(item)

    with open(save_file_name,'w',encoding='utf-8') as fwrite:
        fwrite.write(json.dumps(result,indent=4,ensure_ascii=False))


if __name__ == '__main__':
    convert_csv_to_json(r"D:\迅雷下载\5b7ede1a-c2f2-4862-ba95-b6794a7bd1fc\all.txt",r"D:\迅雷下载\5b7ede1a-c2f2-4862-ba95-b6794a7bd1fc\all.json")


